package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import com.leyou.common.auth.entity.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping(name="向redis中添加购物车数据")
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){
//        获取到用户id
        String userId = UserHolder.getUserId();
        cartService.addCartToRedis(userId,cart);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @GetMapping(value = "/list" ,name = "从redis中获取当前登录人的购物车数据集合")
    public ResponseEntity<List<Cart>> findCartListFromRedis(){
//        获取到用户id
        String userId = UserHolder.getUserId();
        List<Cart> cartList = cartService.findCartListFromRedisByUserId(userId);
        return ResponseEntity.ok(cartList);
    }

    @PutMapping(name="修改某购物车的数量")
    public ResponseEntity<Void> updateCartNum(@RequestParam("id") Long skuId,@RequestParam("num") Integer num){
//        获取到用户id
        String userId = UserHolder.getUserId();
        cartService.updateCartNum(userId,skuId,num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping(value = "/{id}",name="删除购物车")
    public ResponseEntity<Void> deleteCartFromRedis(@PathVariable("id") Long skuId){
//        获取到用户id
        String userId = UserHolder.getUserId();
        cartService.deleteCartFromRedis(userId,skuId);
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/list" ,name = "向redis中批量添加购物车")
    public ResponseEntity<Void> addCartListToRedis(@RequestBody List<Cart> cartList){
//        获取到用户id
        String userId = UserHolder.getUserId();
         cartService.addCartListToRedis(userId,cartList);
        return ResponseEntity.ok().build();
    }

}
